/*
Dijjer - A Peer to Peer HTTP Cache
Copyright (C) 2004,2005  Change.Tv, Inc

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package dijjer.util;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import dijjer.io.WritableToDataOutputStream;

/**
 * @author ian
 *
 * To change the template for this generated type comment go to Window - Preferences - Java - Code Generation - Code and
 * Comments
 */
public class VeryLongInteger implements WritableToDataOutputStream {

    public static final String VERSION = "$Id: VeryLongInteger.java 2 2008-03-21 14:33:03Z ian.clarke $";

	private long _low;
	private long _high;

	public VeryLongInteger(DataInputStream dis) throws IOException {
		_low = dis.readLong();
		_high = dis.readLong();
	}

	public VeryLongInteger(String seed) {
			this(seed.getBytes());
	}

	public VeryLongInteger(byte[] bytes) {
		setToByteArrayDigest(bytes);
	}

	public VeryLongInteger(long low, long high) {
		_low = low;
		_high = high;
	}

	private void setToByteArrayDigest(byte[] bytes) {
		MessageDigest sha1;
		try {
			sha1 = MessageDigest.getInstance("SHA");
			sha1.update(bytes);
			byte[] digest = sha1.digest();
			DataInputStream dis = new DataInputStream(new ByteArrayInputStream(digest));
			_low = dis.readLong();
			_high = dis.readLong();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void writeToDataOutputStream(DataOutputStream stream) throws IOException {
		stream.writeLong(_low);
		stream.writeLong(_high);
	}

	public boolean closerTo(VeryLongInteger a, VeryLongInteger b) {
		boolean ret;
		if (Math.abs(_high - a._high) > Math.abs(_high - b._high)) {
			ret = false;
		}else if (Math.abs(_high - a._high) < Math.abs(_high - b._high)) {
			ret = true;
		} else if (Math.abs(_low - a._low) < Math.abs(_low - b._low)) {
			ret = true;
		}else {
			ret =  false;
		}
		return ret;
	}

	public int getMostSignificantByte() {
		return ((int) (_high >> (8 * 7))) & 0xFF;
	}

	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof VeryLongInteger)) {
			return false;
		}

		final VeryLongInteger veryLongInteger = (VeryLongInteger) o;

		if (_high != veryLongInteger._high) {
			return false;
		}
		if (_low != veryLongInteger._low) {
			return false;
		}

		return true;
	}

	public int hashCode() {
		return ((new Long(_low)).hashCode() >> 1) ^ ((new Long(_high)).hashCode());
	}

	public long getHigh() {
		return _high;
	}

	public long getLow() {
		return _low;
	}

	public String toString() {
		return _low + ":" + _high;
	}
}